package treeedit;

import javax.swing.*;
import javax.swing.text.*;

import sun.print.resources.serviceui_de;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Set;

public class TestFiller {
	JTextField textField = new JTextField();
	JFrame frame = new JFrame ("TestFiller"); 
	String [] keywords = { "cdr","car", "cond", "defun" };
	
	CodeFiller filler = new CodeFiller();
	public TestFiller() {
		frame.setLayout(new FlowLayout());
		frame.setSize(300,300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		textField.setPreferredSize(new Dimension (250,textField.getPreferredSize().height));
		textField.addKeyListener(new FillerPopListener(filler));
		frame.add(textField);
		frame.setVisible(true);
	}
	public static void main(String [] args) {
		TestFiller tf = new TestFiller();
	}

}

class FillerPopListener extends KeyAdapter {
	CodeFiller filler;
	public FillerPopListener(CodeFiller filler) {
		this.filler = filler;
	}
	public void keyPressed(KeyEvent evt) {
		if(evt.getKeyCode()==KeyEvent.VK_DOWN)
			filler.selectDown();
		else if(evt.getKeyCode()==KeyEvent.VK_UP)
			filler.selectUp();
		else if(evt.getKeyCode()==KeyEvent.VK_ESCAPE)
			filler.setVisible(false);
		else if(evt.getKeyCode()==KeyEvent.VK_ENTER)
			((JTextComponent)evt.getSource()).setText(filler.finish());
	}
	public void keyTyped(KeyEvent evt) {
		if(Character.isLetter(evt.getKeyChar()))
			filler.setVisible(true);
		else if(evt.getKeyChar()==' ' && evt.isControlDown()) {
			filler.setVisible(true);
			evt.consume();
		}
		filler.attachTo((Component)evt.getSource());
		String text = ((JTextComponent)evt.getSource()).getText();
		if(Character.isLetter(evt.getKeyChar()))
			text += evt.getKeyChar();
		filler.filter(text);
	}
}
